{% extends 'subpage/base.html' %}
{% block ueditorbox %}
    <script id="editor" type="text/plain"></script>
    <div class="clearfix submit-box">
        <button type="button" class="btn btn-primary btn-submit-confirm pull-left">提交</button>
        <button type="button" class="btn btn-default pull-right cancel-return">取消</button>
    </div>
{% endblock %}
{% block scripts %}
    {{ super() }}
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.config.js') }}"></script>
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.all.min.js') }}"></script>
    <script type="text/javascript"
            src="{{ url_for("static",filename="extra/My97DatePicker/WdatePicker.js") }}"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/lang/zh-cn/zh-cn.js') }}"></script>
    <script src="{{ url_for('static',filename='ueditor/ueditor.parse.js') }}"></script>
    <script>
        setTimeout(function () {
            uParse('div',
                {
                    'highlightJsUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCore.js") }}',
                    'highlightCssUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCoreDefault.css") }}'

                })
        }, 300);
    </script>
{% endblock %}
{% block time_field %}
    <div class="time_field">
        <label for="dutyTime" class="control-label col-xs-4" style="padding-top: 6px;padding-left: 0"><i
                class="require">*</i>日报时间</label>
        <div class="col-xs-8">
            <input type="text" id="dutyTime" class="Wdate"
                   onclick="WdatePicker({maxDate:'%y-%M-%d',dateFmt:'yyyy/MM/dd',alwaysUseStartDate:true})"/>
        </div>
    </div>
{% endblock %}
{% block content %}
    {{ super() }}
    <script>
        $(function () {
            var departId = GetQueryString('departId'), dutyId = GetQueryString('dutyId');
            var ue = UE.getEditor('editor', {
                serverUrl: "/upload",
                initialFrameWidth: 880,  //初始化编辑器宽度,默认1000
                initialFrameHeight: 500
            });

            //动态标题
            var title = GetQueryString("departAlias") + "&nbsp;&nbsp;&nbsp;二线值班报告";//如果编辑状态的话应该放在ajax success回调的方法下
            $('.title-text').html(title);
            if (dutyId) {//修改
                ajaxRequest("GET", GLOBAL_VARIABLE.BASE_URL + '/datashow', {
                    "dutyId": GetQueryString("dutyId"),
                    "departId": GetQueryString("departId"),
                    "level": 2
                }, function (data) {
                    console.log(data);
                    $("#dutyTime").val(data.richTextTime);
                    ue.ready(function () {
                        ue.setContent(data.richText);
                    });
                }, function (error) {
                    $.myAlert({title: "出错啦", message: '请求出错！'});
                });
            } else {//创建
                ue.ready(function () {
                    //查询是否已存在模板
                    ajaxRequest("GET", GLOBAL_VARIABLE.BASE_URL + "/gettemplate", {"departId": departId}, success, error, "{{ csrf_token }}");
                    function success(data) {
                        if (data.code === 0) {
                            ue.setContent(data.data.tempText);
                        } else if (data.code === 1) {
                            $.myAlert({title: '出错提示', message: data.errmessage});
                        }

                    }

                    function error(ero) {
                        $.myAlert({title: "出错提示", message: '请求出错！'});
                    }
                });

            }


            //执行提交操作
            $('.btn-submit-confirm').click(function () {
                var departId = GetQueryString('departId'), departName = GetQueryString('departName'), data = {},
                    datalist = [], duty_time = $("#dutyTime").val();
                //获取富文本编辑器内容
                var u_content = ue.getContent();
                //判断是否日报时间为空
                if (!duty_time || duty_time.length === 0) {
                    $.myAlert({title: ' 出错提示', message: '请填写日报时间！'});
                    return false;
                }
                //判断传的值是否全为空
                if (u_content.length === 0) {
                    $.myAlert({title: '出错提示', message: '请完善富文本框内容！'});
                    return;
                }
                if (dutyId) {//修改
                    data = {
                        "type": "edit",
                        "dutyId": dutyId,
                        "departId": departId,
                        "datalist": datalist,
                        "departName": departName,
                        "substance": u_content,
                        "level": 2,
                        "richTextTime": duty_time
                    };
                } else {//创建
                    data = {
                        "type": "create",
                        "departId": departId,
                        "datalist": datalist,
                        "departName": departName,
                        "substance": u_content,
                        "level": 2,
                        "richTextTime": duty_time
                    };
                }

                function success(data) {
                    if (data.code === 0) {//提交成功
                        var dutyId = GetQueryString("dutyId") || data.dutyId, duty_time = data.richTextTime;
                        window.location.href = '/views?dutyId=' + dutyId + '&departId=' + departId + '&departAlias=' + title + '&duty_time=' + duty_time;
                    } else if (data.code === 1) {
                        $.myAlert({title: '出错提示', message: data.errmessage});
                    }
                }

                function error(err) {
                    $.myAlert({title: '出错提示', message: err.errmessage});
                }

                ajaxRequest('POST', GLOBAL_VARIABLE.BASE_URL + '/datasave', JSON.stringify(data), success, error, '{{ csrf_token }}');
            });

            $(document).on("click", ".cancel-return", function () {
                window.history.go(-1);
            });
        });
    </script>
{% endblock %}